package com.tz.fruit.controller;

import com.tz.fruit.entity.*;
import com.tz.fruit.service.OrderService.OrderService;
import com.tz.fruit.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.HashSet;

import java.util.Set;

/**
 * Created by Administrator on 2017/7/19.
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderservice;

    @RequestMapping("/inquiry")
    public  String inquiry(Model model, HttpServletRequest req, HttpServletResponse resp,String pageNow, String pageSize, String orderNo,String orderStatus, String start, String end,String sign) throws IOException {
        HttpSession session = req.getSession();


        Set<OrderItem> items = new HashSet<>();
        //新建一个页面的对象
        PageBean<Order> pageBean =new PageBean<>();

        //获取用户id
        User user=(User) session.getAttribute("user");

        String []status=new String[]{"已付款","未付款","用户取消","未发货","已发货","已完成"};

        model.addAttribute("status",status);



          /*因为user暂时提供不了*/
       String uid=user.getUserId().toString();


        // 将结算页的订单封装成订单明细 输出来 获取结算页订单的id
        String[] id = (String[]) session.getAttribute("id");
        //orders = orderservice.findAll();


        if(orderNo==null  || orderNo.trim().length()==0){
            orderNo=null;
        }

        if(pageNow==null || pageNow.trim().length()==0){
            pageNow="1";
        }
        if(pageSize==null ||pageSize.trim().length()==0){
            pageSize="2";
        }
        if(start==null || start.toString().trim().length()==0) {
            start=null;
        }if(end==null || end.toString().trim().length()==0){
            end=null;
        }
        //判断类别是否为空
        if(orderStatus==null || orderStatus.trim().length()==0){
            //判断sign  1位降序  0位升序  null或其他位正常查询
            if(sign!=null && sign.equals("0") ) {
                pageBean = orderservice.sortup(Long.valueOf(uid), orderNo,DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, null);
            }else if(sign!=null && sign.equals("1") ){
                pageBean = orderservice.sortdown(Long.valueOf(uid), orderNo, DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, null);
            }else{
                pageBean = orderservice.getBy(Long.valueOf(uid), orderNo, DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, null);
            }
        }else{
            //直接从数据库查询数据
            //判断sign  1位降序  0位升序  null或其他位正常查询
            if(sign!=null && sign.equals("0") ) {
                pageBean = orderservice.sortup(Long.valueOf(uid), orderNo, DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, Enum.valueOf(OrderStatus.class, orderStatus));
            }else if(sign!=null && sign.equals("1")){
                pageBean = orderservice.sortdown(Long.valueOf(uid), orderNo,DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, Enum.valueOf(OrderStatus.class, orderStatus));
            }else{
                pageBean = orderservice.getBy(Long.valueOf(uid), orderNo,DateUtil.parse("MM/dd/yyyy hh:mm:ss", start), DateUtil.parse("MM/dd/yyyy hh:mm:ss", end), pageNow, pageSize, Enum.valueOf(OrderStatus.class, orderStatus));
            }
        }

        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageCount", pageBean.getPageCount());
        model.addAttribute("orderNo", orderNo);
        model.addAttribute("orderStatus",orderStatus);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("sign",sign);
        return "order/order";

    }
       /*agax*/


    @RequestMapping("/delete")
    public void delete(HttpServletRequest  request,HttpServletResponse response,String tt) throws IOException {

        PrintWriter out=response.getWriter();
        String id[];
        if(tt.length()==2){
            id=tt.split(":");
        }else{
            id=tt.substring(0,tt.length()-1).split(":");
        }
        for(int i=0;i<id.length;i++){
            orderservice.deleteById(Long.valueOf(id[i]));
        }

        out.print("ok");
    }




}

